<?php
namespace Tools;

class Page {

    private $total;      //总行数
    private $listRows;   //每页显示行数
    private $pageNum;    //页数
    private $page;       //第几页
    private $listNum=5;  //显示分页码数
    private $limit;
    private $url;


    public function __construct($total,$listRows){
        $this->total    =  $total;
        $this->listRows =  $listRows;
        $this->pageNum  =  ceil($total/$listRows);
        $this->page     =  $this ->setPage($_GET['page']);
        $this->limit    =  $this -> setLimit();
        $this->url      =  $this -> setUrl();

    }


    private function setLimit() {
    	$start = ($this->page-1)*$this->listRows;
        return $start.", $this->listRows";
    }

    private function setPage($page) {
        if(!empty($page)){
            if($page<=$this->pageNum && $page>0){
                return $page;
            }else if($page<=0){
                return 1;
            }else if($page>$this->pageNum){
                return $this->pageNum;
            }
        }else {
            return 1;
        }
    }
            
    
    function getLimit() {
    	return $this->limit;
    }

    
    private function setUrl(){
       	$url = $_SERVER['REQUEST_URI'];
       	$arrUrl = parse_url($url);

       	if(isset($arrUrl['query'])) {
       		$url = str_replace($arrUrl['query'],'',$url);
       	}
       	$url = $url.((strpos($url,'?'))? '':"?");
       	return $url;
    }


    private function first() {
    	$html="";
    	if($this->page==1) {
    		$html.='';
    	}else {
    		$html .= "<a href='{$this->url}page=1' >首页</a>";
    	}
    	return $html;
    }


    private function last() {
    	$html = "";
    	if($this->page==$this->pageNum) {
    		$html = '';
    	}else {
    		$html .="<a href='{$this->url}page={$this->pageNum}' >尾页</a>";
    	}
    	return $html;
    }


    private function over() {
    	$html = "";
    	if($this->page<=1) {
    		$html = '';
    	}else {
    		$html = "<a href='{$this->url}page=".($this->page-1)."' >上一页</a>";
    	}
    	return $html;
    }


    private function lower() {
    	$html = "";
    	if($this->page>=$this->pageNum) {
    		$html = '';
    	}else {
    		$html = "<a href='{$this->url}page=".($this->page+1)."' >下一页</a>";
    	}
    	return $html;
    }


    private function pageList() {
    	$html = "";
    	$inum = floor($this->listNum/2);

    	for($i=$inum; $i>=1;$i--) {
    		$page = $this->page-$i;

    		if($page<1) {
    			continue;
    		}

    		$html .= "&nbsp;<a href='{$this->url}page={$page}' >{$page}</a>&nbsp;";
    	}

    	$html .= "&nbsp;".$this->page."&nbsp;";

    	for($i=1;$i<=$inum;$i++) {
    		$page = $this->page+$i;

    		if($page<=$this->pageNum) {
    			$html .= "&nbsp;<a href='{$this->url}page={$page}' >{$page}</a>&nbsp;";
    		}else {
    			break;
    		}
    	}

    	return $html;
    }


    private function goPage() {
    	$html = "";
    	$html .= "<form action='{$this->url}' method='GET'><input type='text' name='page'><input type='submit' value='确定'></form>";
        return $html;
    }


    function fpage($display=array(0,1,2,3,4,5,6,7)) {
    	$html[0] = "共有".$this->total."条&nbsp;&nbsp;";
    	$html[1] = "第".$this->page."/".$this->pageNum."页&nbsp;&nbsp;";
    	$html[2] = $this->first()."&nbsp;";
    	$html[3] = $this->over()."&nbsp;";
    	$html[4] = $this->pageList()."&nbsp;";
    	$html[5] = $this->lower()."&nbsp;";
    	$html[6] = $this->last()."&nbsp;";
    	$html[7] = $this->goPage();

        $list = "";
    	foreach($display as $i) {
    		$list .= $html[$i];
    	}

    	return $list;
    }
}
?>